<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>语音识别</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
  </style>
</head>

<body>
  <div></div>
</body>
<script>
  "use strict"; {
    // https://www.cnblogs.com/webzhangnan/archive/2012/12/23/2829985.html
    const newRecognition = new window.webkitSpeechRecognition();
    newRecognition.continuous = true;
    // 开始录音
    newRecognition.start();
    // 关闭录音
    // newRecognition.stop();
    // 监听到错误的信息
    newRecognition.addEventListener("error", err => console.error(err));
    // 开始接受音频
    newRecognition.addEventListener("soundstart", res => {

    });
    // 开始说话
    newRecognition.addEventListener("speechstart", res => {

    });
    // 说话完毕
    newRecognition.addEventListener("speechend", res => {

    });
    // 当成功接收到一段可以识别的语音时触发(可能会触发多次)
    newRecognition.addEventListener("result", e => {
      console.log(event.results[0][0].transcript, 'onresult');
      console.log(e.results);
    });
  };
</script>

</html>